package cate.game.role.res.guild.po.my;

import cate.common.table.d.GDGuild;
import cate.common.util.GameResult;
import cate.common.util.TimeTool;
import cate.game.role.RoleDependent;
import cate.game.role.res.guild.msg.MyGuildResp;
import cate.game.role.res.guild.po.GuildPO;
import cate.game.role.res.guild.po.donate.MyGuildDonatePO;
import cate.game.role.res.guild.po.member.GuildMemberDetailPO;
import cate.game.role.res.guild.po.my.mall.MyGuildMallPO;
import cate.game.role.res.guild.po.my.skill.MyGuildSkillPO;
import cate.game.role.res.guild.po.treasurehouse.MyGuildTreasureHouse;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import org.apache.commons.lang3.StringUtils;
import cate.game.role.Role;

@NoteClass("我的公会")
public class MyGuildPO extends RoleDependent {
    @NoteField(value = "我的公会ID")
    public String uid;

    @NoteField(value = "我的公会名称")
    public String guildName;

    @NoteField(value = "在公会中的表现")
    public MyGuildPerformancePO performance;

    @NoteField(value = "在公会中的捐献")
    public MyGuildDonatePO donate;

    @NoteField(value = "上次退会时间", detail = "退出公会12个小时内无法加入其它公会，但是第一次不受影响")
    public long withdrawalTime;

    @NoteField(value = "我的公会申请")
    public MyGuildApplyPO apply;

    @NoteField(value = "我的公会红包")
    public MyGuildRedEnvelopePO envelope;

    @NoteField(value = "我的公会活跃")
    public MyGuildActivePO active;

    @NoteField(value = "我的公会商店")
    public MyGuildMallPO mall;

    @NoteField(value = "我的公会技能")
    public MyGuildSkillPO skill;

    @NoteField(value = "我的公会宝库")
    public MyGuildTreasureHouse treasureHouse;

    @NoteField(value = "是否有退会惩罚", detail = "第一次不受惩罚")
    private boolean isPunish;

    @NoteField(value = "本次加入公会的时间", detail = "老玩家这个值都是0 没有记录 之后新加公会才会有")
    public long joinTime;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (performance == null) {
            performance = new MyGuildPerformancePO();
        }
        performance.initialize(role);
        if (donate == null) {
            donate = new MyGuildDonatePO();
        }
        donate.initialize(role);
        if (apply == null) {
            apply = new MyGuildApplyPO();
        }
        apply.initialize(role);
        if (envelope == null) {
            envelope = new MyGuildRedEnvelopePO();
        }
        envelope.initialize(role);
        if (active == null) {
            active = new MyGuildActivePO();
        }
        active.initialize(role);
        if (mall == null) {
            mall = new MyGuildMallPO();
        }
        mall.initialize(role);
        if(skill == null){
            skill = new MyGuildSkillPO();
        }
        skill.initialize(role);
        if(treasureHouse == null){
            treasureHouse = new MyGuildTreasureHouse();
        }
        treasureHouse.initialize(role);
    }

    @Override
    public void onEnter() {
        update();
//        affairs.onEnter();
        apply.onEnter();
        donate.onEnter();
    }

    @Override
    public void offline() {
        // 同步公会成员表现
        GuildMemberDetailPO member = getMember();
        if (getMember() != null) {
            member.it.read(role);
        }
    }

    public void update() {
        // 判断有没有加入公会
        if (StringUtils.isBlank(this.uid)) {
            return;
        }
        // 判断公会是否被解散 或者 你被踢了
        if (getGuild() == null) {
            if (!role.getGame().res.guild.data.disbandMap.containsKey(role.getUid())) {
                //无惩罚时间但是需要清空
                long time = System.currentTimeMillis() - TimeTool.TimeCons.DAY;
                clear(time);
                return;
            }
            long time = role.getGame().res.guild.data.disbandMap.get(this.uid);
            clear(time);
            return;
        }
        if (getMember() == null) {
            if (!role.getGame().res.guild.data.kickOutMap.containsKey(role.getUid())) {
                //无惩罚时间但是需要清空
                long time = System.currentTimeMillis() - TimeTool.TimeCons.DAY;
                clear(time);
                return;
            }
            long time = role.getGame().res.guild.data.kickOutMap.get(role.getUid());
            role.getGame().res.guild.data.kickOutMap.remove(role.getUid());
            clear(time);
            return;
        }
        // 确认你还在公会里则进行数据同步
        guildName = getGuild().basic.guildName;
        // 同步公会成员表现
        GuildMemberDetailPO member = getMember();
        performance.read(member.performance);
        // 把主角快照同步到公会
        member.it.read(role);
    }

    /**
     * 清理公会数据
     */
    @JsonIgnore
    public void clear(long time) {
        this.uid = null;
        this.guildName = null;
        this.performance.clear();

        if (isPunish) {
            withdrawalTime = time;
        } else {
            // 首次离开公会时,不记录退会时间
            isPunish = true;
        }
    }

    /**
     * 检查我的公会信息
     */
    @JsonIgnore
    public GameResult<Void> checkGuild() {
        //此处不需要输出
        GameResult<Void> r = new GameResult<>();
        if (StringUtils.isBlank(uid)) {
            return r.fail("尚未加入公会");
        }
        if (getGuild() == null) {
            return r.fail("公会不存在");
        }
        if (getMember() == null) {
            return r.fail("公会中不存在此成员");
        }
        return r.success();
    }

    /**
     * 获取公会信息
     */
    @JsonIgnore
    public GuildPO getGuild() {
        if (StringUtils.isBlank(this.uid)) {
            return null;
        }
        return role.getGame().res.guild.getGuild(this.uid);
    }


    /**
     * 获取我在公会的信息
     */
    @JsonIgnore
    public GuildMemberDetailPO getMember() {
        GuildPO guild = this.getGuild();
        if (guild == null) {
            return null;
        }
        return this.getGuild().member.getMember(role.getUid());
    }


    /**
     * 获取我在公会的职位
     */
    @JsonIgnore
    public byte getPos() {
        GuildMemberDetailPO member = this.getMember();
        if (member == null) {
            return GDGuild.position.MEMBER;
        }
        return member.performance.position;
    }


    /**
     * 清理公会数据
     */
    @JsonIgnore
    public void clear() {
        this.uid = null;
        this.guildName = null;
        this.performance.clear();

        if (isPunish) {
            withdrawalTime = System.currentTimeMillis();
        } else {
            // 首次离开公会时,不记录退会时间
            isPunish = true;
        }
        role.getTrace().leaveGuild();
    }

    /**
     * 我的公会数据更新
     */
    @JsonIgnore
    public void noticeUpdate() {
        // 检查申请信息有没有过期
        if (!checkGuild().ok()) {
            apply.checkExpire();
        }
        role.sendNow(new MyGuildResp(this));
    }

    @Override
    public void onDaySpan(boolean silence) {
        donate.onDaySpan();
        envelope.onDaySpan();
        active.onDaySpan();
        mall.onDaySpan();
        donate.onDaySpan();
        performance.onDaySpan(silence);
        treasureHouse.onDaySpan(silence);
    }

    public void onWeekSpan() {
        active.onWeekSpan();
    }

    /**
     * 加入公会的天数
     */
    public int getJoinDay() {
        if (getGuild() == null) {
            return -1;
        }
        return TimeTool.getDistanceDays(joinTime, game().time.now()) + 1;
    }
}
